import React, { PureComponent } from 'react';
import _ from 'lodash';
import { TableC } from '../../../comps';
import moment from 'moment';
import { connect } from 'dva';
import ImportModal from './ImportModal';
@connect(({ dataImportM }) => ({ dataImportM }))
export default class OTCOptionLifeCycleTable extends PureComponent {
  constructor(props) {
    super(props);
    this.state = {
      selectedRowKeys: [], //选中数据
      tableData: [],
      loading: false,
      modalVisible: false,
    };
    props.onRef(this);
  }
  componentDidMount() {
    this.getData(this.props.date);
  }
  UNSAFE_componentWillReceiveProps(nextProps) {
    if (this.props.date !== nextProps.date) {
      this.getData(nextProps.date);
    }
  }
  // 获取数据
  getData = (date, func) => {
    this.setState({
      loading: true,
    });
    this.props.dispatch({
      type: 'dataImportM/fetch',
      payload: {
        importDate: date,
      },
      callback: (type, data) => {
        if (type == 'success') {
          this.setState({
            tableData: data.data,
            loading: false,
            selectedRowKeys: [],
          });
          this.props.rowSelectionChange([], []);
        } else {
          this.setState({
            tableData: {},
            loading: false,
          });
        }
        func && func();
      },
    });
  };
  //  点击删除
  onDelete = (row) => {
    if (row) {
      this.setState({
        deleteLoading: true,
      });
      this.props.dispatch({
        type: 'dataImportM/deleteTAdate',
        payload: [row.id],
        callback: (type) => {
          if (type == 'success') {
            this.getData(this.props.date);
          }

          this.setState({
            deleteLoading: false,
          });
        },
      });
    }
  };
  handCloseModal = (type) => {
    this.setState({ modalVisible: false });
    if (type == 'success') {
      this.getData(this.props.date);
    }
  };
  //是否禁用
  checkDisable = (row) => {
    const time = moment().format('YYYYMMDD');
    return row.importDate != time || row.isHandle == '1' || row.isHandle2 == '1' || (time != row.settlementDate && time != row.handleDate);
  };

  render() {
    const { tableData, loading, selectedRowKeys, modalVisible, modalData } = this.state;
    const { columns = [], dataSource = [] } = tableData;
    const newColumns =
      columns.length > 0
        ? [
            ...columns.map((x) => {
              if (x.key === 'businessType') {
                return {
                  ...x,
                  width: '120px',
                  render: (text, record) => {
                    //申购(02)、赎回(03)、基金成立(50)、基金转换出(13)、
                    //基金转换入(16)、现金分红（74）、再投资1（73）、再投资2（75）、强增(70)、强减(71)
                    //14快速赎回
                    if (text == '02') {
                      return '申购';
                    } else if (text == '03') {
                      return '赎回';
                    } else if (text == '50') {
                      return '基金成立';
                    } else if (text == '13') {
                      return '基金转换出';
                    } else if (text == '16') {
                      return '基金转换入';
                    } else if (text == '74') {
                      return '现金分红';
                    } else if (text == '73') {
                      return '再投资1';
                    } else if (text == '75') {
                      return '再投资2';
                    } else if (text == '70') {
                      return '强增';
                    } else if (text == '71') {
                      return '强减';
                    } else if (text == '14') {
                      return '快速赎回';
                    }
                  },
                };
              } else if (
                ['turnoverShare', 'turnoverAmount', 'totalCost', 'transactionFee', 'trusteeshipFee', 'stampDuty', 'afterCharge', 'otherExpenses', 'gjjzcCost', 'unitBonus'].indexOf(
                  x.key,
                ) > -1
              ) {
                return {
                  ...x,
                  width: '120px',
                  type: 'money',
                };
              } else if (x.key === 'isHandle2' || x.key === 'isHandle') {
                return {
                  ...x,
                  width: '100px',
                  render: (text) => {
                    if (text == '1') {
                      return '已处理';
                    } else {
                      return '未处理';
                    }
                  },
                };
              } else if (x.key === 'exchangeDate' || x.key === 'settlementDate' || x.key === 'handleDate' || x.key === 'importDate') {
                return {
                  ...x,
                  width: '120px',
                  render: (text) => {
                    if (text) {
                      return moment(text).format('YYYY-MM-DD');
                    } else {
                      return '--';
                    }
                  },
                };
              } else {
                x.width = '180px';
              }
              return x;
            }),
            {
              title: '操作',
              dataIndex: 'edit',
              key: 'edit',
              type: 'operate',
              fixed: 'right',
              width: 110,
              operates: [
                {
                  title: '修改',
                  onClick: (record) => this.setState({ modalVisible: true, modalData: record }),
                  rules: {
                    disabled: {
                      items: [
                        { key: 'importDate', value: [moment().format('YYYYMMDD')], relation: ['!='] },
                        { key: 'isHandle', value: ['0'], relation: ['!='] },
                        { key: 'isHandle2', value: ['0'], relation: ['!='] },
                      ],
                      relation: 'or',
                    },
                  },
                },
                {
                  title: '删除',
                  onClick: (record) => {
                    this.onDelete(record);
                  },
                  rules: {
                    disabled: {
                      items: [
                        { key: 'importDate', value: [moment().format('YYYYMMDD')], relation: ['!='] },
                        { key: 'isHandle', value: ['0'], relation: ['!='] },
                        { key: 'isHandle2', value: ['0'], relation: ['!='] },
                      ],
                      relation: 'or',
                    },
                  },
                },
              ],
            },
          ]
        : [];

    const rowSelection = {
      selectedRowKeys,
      onChange: (selectedRowKeys, selectedRows) => {
        this.setState({
          selectedRows,
          selectedRowKeys,
        });
        this.props.rowSelectionChange(selectedRowKeys, selectedRows);
      },
    };
    return (
      <>
        <div style={{ marginTop: '20px' }}>
          <TableC
            columns={newColumns}
            dataSource={dataSource || []}
            scroll={{ x: '100%' }}
            selfScrollY={true}
            rowKey='id'
            rowSelection={rowSelection}
            loading={loading}
            hidePage={true}
          />
        </div>
        <ImportModal visible={modalVisible} data={modalData} handCloseModal={(e) => this.handCloseModal(e)}></ImportModal>
      </>
    );
  }
}
